package com.smalls.newvideotwo.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 项目名称：NewVideoFour
 * 类描述：
 * 创建人：Sophia Huang
 * 创建时间：2018/4/24 22:48
 * 修改人：Sophia Huang
 * 修改时间：2018/4/24 22:48
 * 修改备注：
 */
public class MyLinearLayoutManager extends LinearLayoutManager {

    public MyLinearLayoutManager(Context context) {
        super(context);
    }

    public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    @Override
    public View onInterceptFocusSearch(View focused, int direction) {
        int count = getItemCount();
        int fromPos = getPosition(focused);
        switch (getOrientation()) {
            case HORIZONTAL:
                switch (direction) {
                    case View.FOCUS_RIGHT:
                        fromPos++;
                        break;
                    case View.FOCUS_LEFT:
                        fromPos--;
                        break;
                    default:
                        break;
                }
                break;
            case VERTICAL:
                switch (direction) {
                    case View.FOCUS_DOWN:
                        fromPos++;
                        break;
                    case View.FOCUS_UP:
                        fromPos--;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

        if (fromPos < 0 || fromPos >= count) {
            return focused;
        }
        return super.onInterceptFocusSearch(focused, direction);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}
